package com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.adhoc;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.BitAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.internalDownload;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Site_Info;

/**
 * @ClassName bitauto
 * @Description TODO
 * @Author songding
 * @Date 2021/9/26 10:05
 * @Version 1.0
 * 易车车系临时采集
 *
 **/
public class BitAutoCarTypeSimpleCrawlerScheduleJob extends BitAutoCommonCrawlerSchedulerJob {
    public static void main(String[] args) {
        publishBitAutoCrawlerSchedulerJobInfo();
    }

    public static CrawlerJob publishBitAutoCrawlerSchedulerJobInfo(){
        CrawlerJob bitAutoScheduler = bitAutoScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(bitAutoScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("易车");
        crawlerScheduleJob.setCrawlerKey(bitAutoScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return bitAutoScheduler;
    }

    public static CrawlerJob bitAutoScheduler(){
        String url = "https://car.yiche.com/";
        String site = "carType";
        String site_biz = "simple";
        String site_info = "simple-2021/09/26";
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl(url)
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .domain(domain)
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(domain))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*27,  null))
                .proxy(proxy)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();

        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);
        requestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,site_info);

        //关键词 source record
        CrawlerRequestRecord keywordCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("bitauto_keywords",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/bitauto/keys?site=typeSimple")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),//bitauto-carType-simple-simple-2021/09/26-realtime
                        StringUtils.joinWith("-",site,site_biz,site_info,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain, site,site_biz,site_info,"queue")))
                .kafkaResultPipeline("kafka",kafkaTopciForTraceJob,null)
                .crawlerJobThreadNumber(10)
                .requestRecord(requestRecord)
                .supportRecord(keywordCrawlerRecord)
                .build();
    }


}
